package com.chang.soul.controller;


import com.alibaba.fastjson.JSONObject;
import com.chang.soul.bean.ArticleBean;
import com.chang.soul.bean.ArticleQueryForm;

import com.chang.soul.conf.JwtIgnore;
import com.chang.soul.service.ArticleService;
import com.chang.soul.util.Result;
import com.chang.soul.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/article")
@Api("文章管理接口")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @ApiOperation(value = "分页模糊查询文章接口",notes = "",httpMethod = "POST")
    @PostMapping("/getAllArticleList")
    @JwtIgnore
    public Result getAllArticleList(@Valid @RequestBody ArticleQueryForm articleQueryForm){
        System.out.println(articleQueryForm);
        Integer currentPage = articleQueryForm.getCurrentPage();
        Integer pageSize = articleQueryForm.getPageSize();
        currentPage =currentPage == null || currentPage <= 0 ? 1 : currentPage;
        pageSize = pageSize== null || pageSize <= 0 ? 10 : pageSize;
        Integer startNum = (currentPage -1)*pageSize;
        articleQueryForm.setCurrentPage(startNum);
        articleQueryForm.setPageSize(pageSize);

        List<ArticleBean> list = articleService.getAllArticleList(articleQueryForm);
        System.out.println("list"+list);
        int total = articleService.getCountByArticleList(articleQueryForm);
        JSONObject res = new JSONObject();
        res.put("currentPage",currentPage);
        res.put("pageSize",pageSize);
        res.put("totalCount",total);
        res.put("list",list);

        System.out.println(res);
        return ResultUtil.success(res);
    }

    @ApiOperation(value = "插入文章",notes = "",httpMethod = "POST")
    @PostMapping("/insertArticle")
    @JwtIgnore
    public Result insertArticle(@Valid @RequestBody ArticleBean articleBean) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(new Date())); // 获取当前系统时间
        articleBean.setCreateTime(df.format(new Date()));
        boolean insert_flag = articleService.insertArticle(articleBean);
        if (insert_flag) {
            return ResultUtil.success(articleBean);
        } else {
            return ResultUtil.error("insert article fail!");
        }
    }

    @ApiOperation(value = "删除文章",notes = "",httpMethod = "GET")
    @GetMapping("/deleteArticleById")
    @JwtIgnore
    public Result deleteArticleById(@RequestParam(value = "id") Integer id){
        boolean flag = articleService.deleteArticleById(id);
        if(!flag){
            return ResultUtil.error("delete article fail");
        }
        return ResultUtil.success("delete article success!");
    }

    @ApiOperation(value = "更新文章",notes = "",httpMethod = "POST")
    @PostMapping("/updateArticle")
    @JwtIgnore
    public Result updateArticle(@Valid @RequestBody ArticleBean articleBean){
        boolean up_flag = articleService.updateArticle(articleBean);
        if(!up_flag){
            return ResultUtil.error("update article fail");
        }
        return ResultUtil.success("update article success!");
    }

    @ApiOperation(value = "查看文章详情",notes = "无需token",httpMethod = "GET")
    @GetMapping("/getArticleById")
    @JwtIgnore
    public Result getArticleById(@RequestParam(value = "id") Integer id){
        ArticleBean articleBean = articleService.getArticleById(id);
        if(articleBean == null){
            return ResultUtil.error("此id 对应内容为空，请重新输入");
        }
        return ResultUtil.success(articleBean);
    }

}
